package stateDiagram.EntityObjects;

import java.util.ArrayList;

import stateDiagram.figures.TransitionFigure;

public class TransitionEntity {
	
	private String eventName;
	private StateEntity fromState;
	private StateEntity toState;
	private TransitionFigure transitionFigure;
	//private ArrayList <String> actions; - for when events can have multiple actions
	private String action;

	
	/**
	 * Constructor for a new TransitionEntity, adds the appropriate reference to the root and destination of this transition
	 * @param name -- a String representing the conditions of this TransitionEntity's execution
	 * @param prev -- the StateEntity that this transition begins in
	 * @param next -- the StateEntity that this transition ends in
	 */
	public TransitionEntity(String name, TransitionFigure figure)
	{
		setName(name);
		//actions = new ArrayList<String>();
		transitionFigure = figure;	
	}
	
	/**
	 * Returns the name of this TransitionEntity
	 * @return the name of this Transition Entity
	 */
	public String getName() 
	{
		
		return eventName;
	}

	/**
	 * Sets the name of this transition entity
	 * @param name the new name of this transition entity
	 */
	public void setName(String newName) 
	{
		if (newName == null) newName = "";
		eventName = newName;
	}
	
	/**
	 * returns the StateEntity that this TransitionEntity begins in
	 * @return the root StateEntity
	 */
	public StateEntity getLastState() 
	{
	
		return fromState;
	}
	/*
	 * set the state transition originated from
	 */
	public void setFromState(StateEntity fromState){
		this.fromState = fromState;
	}
	/**
	 * Returns the StateEntity that this TransitionEntity points to
	 * @return the StateEntity that is next
	 */
	public StateEntity getNextState() 
	{
		return toState;
	}
	/*
	 * set state transition goes to
	 */
	public void setToState(StateEntity toState)
	{
		this.toState = toState;
		
	}
	
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * Outputs a string in XML format representing this TransitionEntity's data
	 * @return a String representing this TransitionEntity
	 */
	public String toXML()
	{ 
		String XML = "";
		if(getNextState() != null && getLastState() != null){
		XML = XML + ("\t\t\t<Transition>\n\t\t\t<TransitionEvent>"+ eventName +"</TransitionEvent>\n\t\t\t<LastState>" + getLastState().getName() + "</LastState>\n\t\t\t<NextState>" + 
				getNextState().getName() +"</NextState>\n\t\t\t\t<Actions>\n");	
		}
		else{XML = XML + ("\t\t\t<Transition>\n\t\t\t<TransitionEvent>"+ eventName +"</TransitionEvent>\n\t\t\t\t<Actions>\n");
		}
		//for(String s : actions){
			XML = XML + ("\t\t\t\t\t<action>" + action + "</action>\n");
		//}
		XML =XML + ("\t\t\t\t</Actions>\n\t\t\t</Transition>");
		return XML;
	}
	/**
	 * Returns whether or not this TransitionEntity is similar to another
	 * @param other -- another TransitionEntity to compare to
	 * @return True if all fields in this TransitionEntity are equal to the parameter "other"
	 */
	public boolean equals(TransitionEntity other)
	{
		if (this.eventName == other.getName() && this.fromState == other.fromState && this.toState == other.toState) return true;
		else return false;
	}
	
	public boolean isInternalTransition()
	{
		return (getNextState() instanceof SubStateEntity);
	}
	
	public TransitionFigure getFigure()
	{
		return transitionFigure;
	}




}
